<template>
  <div class="search-input">
    <div class="search-inner">
      <i class="iconfont purple-search"></i>
      <input
        type="text"
        v-model="inputValue"
        @input="inputCb($event)"
        placeholder="请输入歌曲、歌手"
      />
      <i v-show="inputValue" class="iconfont purple-close" @click="clear"></i>
    </div>
  </div>
</template>

<script>
import useModel from './useModel'
export default {
  name: 'SearchInput',
  props: {
    modelValue: {
      type: String,
      default: ''
    }
  },
  watch: {
    modelValue(newValue) {
      this.inputValue = newValue
    }
  },
  emits: ['update:modelValue'],
  setup(props, { emit }) {
    const { inputValue, clear, inputCb } = useModel(props, emit)

    return {
      clear,
      inputValue,
      inputCb
    }
  }
}
</script>

<style lang="scss" scoped>
.search-input {
  display: flex;
  justify-content: center;
  padding: 20px 0;
  .search-inner {
    position: relative;
    width: 85%;
    height: 32px;
    background-color: #fff;
    border-radius: 16px;
    border: none;
    > .iconfont {
      position: absolute;
      &.purple-search {
        left: 10px;
        top: 50%;
        transform: translate3d(0, -50%, 0);
      }
      &.purple-close {
        right: 10px;
        top: 50%;
        transform: translate3d(0, -50%, 0);
      }
    }
    > input {
      width: 100%;
      height: 100%;
      padding: 0 35px;
      font-size: 14px;
      border: none;
      background-color: transparent;
      &:focus {
        outline: none;
      }
    }
  }
}
</style>
